home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Pascal / Applications / P4⁄Mac 2.0d4 / Mac source 2.0 / TransSkel.p < prev   
Encoding:
Text File  |  1996-09-28  |  52.6 KB  |  1,728 lines  |  [TEXT/PJMM]

  1. {    TransSkel version 2.00 - Transportable application skeleton}
  2.  
  3. {    TransSkel is public domain and was originally written in LightSpeed C by:}
  4.  
  5. {            Paul DuBois}
  6. {            Wisconsin Regional Primate Research Center}
  7. {            1220 Capital Court}
  8. {            Madison WI  53706  USA}
  9.  
  10. {    UUCP:    [allegra,ihnp4,seismo]!uwvax!rhesus!dubois }
  11. {    ARPA:    dubois@rhesus.primate.wisc.edu}
  12. {    The Pascal Version of TransSkel is public domain and was ported and changed by        }
  13.  
  14. {            Owen Hartnett            }
  15. {            Ωhm Software            }
  16. {            163 Richard Drive        }
  17. {            Tiverton, RI 02878        }
  18.  
  19. {    CSNET:    omh@cs.brown.edu.CSNET                                             }
  20. {    ARPA:        omh%cs.brown.edu                                                    }
  21. {    UUCP:        [ihnp4,allegra]!brunix !omh                                            }
  22.  
  23. {    This version of TransSkel written for Lightspeed Pascal.  Lightspeed Pascal is a}
  24. {    trademark of:}
  25. {            THINK Technologies, Inc}
  26. {            420 Bedford Street  Suite 350}
  27. {            Lexington, MA  02173  USA}
  28.  
  29. {  History}
  30. {  06/13/86    Beta version. (pd) }
  31. {  08/27/86    Version number changed to 1.01.(pd)}
  32. {              v1.0 DoGrow bug fixed - the port at the point of the}
  33. {              InvalRect could have been anything; the fix is to set}
  34. {              the port to the grown window first.  This also explains}
  35. {              why the kludge to DoActivate in v1.0 worked.(pd)}
  36. {  10/02/86    Version number changed to 1.02, as a result of adding}
  37. {            modifications by David W. Berry (well!dwb@lll-lcc.arpa)}
  38. {            for supporting window zooming.  Also used his modifications}
  39. {            for supporting modeless dialogs (though not in the same}
  40. {            form).  Dialogs can be #define'd on or off.(pd)}
  41.  
  42. {12/ 28 / 86 Version number changed to 1.03 . Modified to work under LightspeedC v . 2.01 }
  43. {            - took out definitions for window zooming stuff , as it is now supported by the compiler}
  44. {             directly . Also declared DoZoom static , fixing an oversight . ( pd )}
  45. { 01 / 18 / 86 Put a SetPort into DoZoom - ZoomWindow requires port to be}
  46. {            set to window being zoomed . ( pd )}
  47. { 02 / 05 / 86 Version number changed to 1.04 . Big change : port setting behavior made explicit}
  48. {             - the only persistant switch occurs when a window comes active . This changes }
  49. {            underlying programming model ( see manual for detailed discussion ) . Thanks to}
  50. {             Duane Williams for pointing out that this should be done . Typedef 'd }
  51. {            integer/long variables to Integer, Longint to facilitate coversion to other C }
  52. {            compilers . More complete type-casting done . LightspeedC does a lot of it }
  53. {            automatically , other compilers may not . ( pd - this version never released ) }
  54. {03 / 02 / 87 Fixed bug whereby clicks in drag region of non - active windows may not bring }
  55. {            window to front . Seems to be due to DragWindow calling StillDown to see    if mouse is still }
  56. {            down .    If the machine was busy    otherwise when click occurred and }
  57. {            mouse already up when DragWindow is called , the click ends up being ignored . }
  58. {            Thanks to Roger Humphrey for finding this one . }
  59.  
  60. {* * * Changes implemented first by omh to Pascal Version}
  61.  
  62. { 12/24/86  Finished first Pascal version.   Dialogs cannot be defined off.     (omh)}
  63.  
  64. {4 / 18 / 87 Changed Desk Accessory code so it 's more tolerant of memory}
  65. {            conditions for desk accessories . ( omh ) }
  66. {7 / 12 / 87 Added "cache " code to GetWDHandler . Now TransSkel figures }
  67. {            that an event is most likely to occur for the same window as the previous }
  68. {            event . Thus the WindowPtr and WDHandle for events are cached and examined }
  69. {            to avoid searching through the handler list . ( omh ) }
  70. {7 / 12 / 87 Excised the notorious "SetPort "excess . As pointed out by Duane Williams ,}
  71. {             SetPort traps abounded unnecessarily in version 1.02 . These have been eliminated }
  72. {            now with two exceptions . First , the port is set when a window handler }
  73. {            is installed . The justification for this is that when a handler is installed , it }
  74. {            is likely that further processing will be done on it immediately . The application gets }
  75. {            control immediately after the handler is installed anyway , so this behavior can be manually }
  76. {            overridden where necessary . Second , when a window is activated , the port is}
  77. {            set to it . This follows the model of keeping the port in sync with the }
  78. {            active window . ( omh ) }
  79. {7 / 14 / 87 Added grow zone function installation    and MoreMasters to SkelInit , }
  80. {            which now requires two parameters . The first indicates the number of times to call }
  81. {            MoreMasters . The second is a ProcPtr indicating a user - supplied grow zone}
  82. {            function to be called when memory problems occur .  If nil , no grow zone}
  83. {            function is    installed . ( omh ) }
  84. {7 / 14 / 87 SkelMenu , SkelWindow , and SkelDialog now return zero or non - zero to indicate }
  85. {            failure or success of handler allocation . This could break * all * previous TransSkel }
  86. {            applications ( as will the change to SkelInit , above . Please see the section "How to }
  87. {            adapt old TransSkel to New "    in the manual    for detailed specifications on }
  88. {            how to convert your old programs . TransSkel becomes more memory conscious}
  89. {            with these changes . The functions SkelMenu , SkelWindow , and }
  90. {            SkelDialog are the only routines which actually allocate memory . Since they may be }
  91. {            called at any time , knowing that you have enough memory becomes important . Thus , }
  92. {            these routines return a value to indicate what happened .  If they return zero ,}
  93. {             then memory allocation failed . ( omh ) }
  94. {10 / 21 / 87 Added another parameter to SkelMenu:  drawBar: Boolean . This tells SkelMenu }
  95. {            whether to draw the menu bar after adding the Menu . This is done to eliminate }
  96. {            the menus popping up one at a time . Simply call SkelMenu  with drawBar false}
  97. {            until the    last time you call SkelMenu , then call it (for the last menu )}
  98. {            with drawBar true . ( omh ) }
  99. {10 / 26 / 87 Removed declarations for zoom - in and zoom - out . Added Pascal }
  100. {            changes ( above ) to C version . ( omh )}
  101.  
  102. {    02 / 02 / 88 Merged pd 's 1.04 changes with those of omh, above, to create}
  103. {            release version 2.0 . Fixed bug whereby cmd - key equivalents}
  104. {            for menu selections would execute twice if DA window in front . Thanks }
  105. {            to Don Fredkin and Julian Vrieslander for finding this one , and to Don for the}
  106. {             best fix . ( pd ) }
  107. { 10/28/88 Removed all New Rom calls. }
  108. { 10/28/88 Added support for conditional compilation for dialogs and MPW support.  By setting }
  109. {            the Think_pascal flag to false, TransSkel will run under MPW. Now correctly written for LSP 2.0}
  110.  
  111. {Some fixes done later by Ingemar Ragnemalm. Search for "Ingemar" fo find these.}
  112. {They include some bug fix and hierarcical menu support (call SkelHMenu instead of SkelMenu}
  113. {for those menus).}
  114.  
  115. {New fix 18/9-93: Added a filter proc for dialogs, to allow special event processing before}
  116. {an event is passed to IsDialogEvent, when a modeless dialog is in the front. This makes it possible}
  117. {to use user items in modeless dialogs, and to handle return/enter.}
  118.  
  119. {Added WNE-support. Use SkelSetSleep and SkelSetMouseRgn if you need them. /Ingemar}
  120.  
  121. {Added FindWindowByRefcon, a routine that is useful for certain multi-window appplications.}
  122.  
  123. {july -94: Plugged in suspend/resume-handlers, conforming with TransSkel 3.0. (Handler should}
  124. {take a boolean as parameter. If the boolean is true, the application was resumed, otherwise it was}
  125. {suspended.) Processes Apple Events by calling AEProcessAppleEvent for you.}
  126.  
  127. unit TransSkel;
  128.  
  129. interface
  130.  
  131. {$IFC UNDEFINED GENERATINGPOWERPC }
  132. {$SETC GENERATINGPOWERPC:= false }
  133. {$ENDC}
  134.  
  135. {$SETC supportDialogs:= true }
  136.                     { Set to false to disallow modeless dialog support and save code space }
  137.                 { Set to false to have SkelInit call QuickDraw Inits: InitGraf, InitDialog, etc. }
  138.  
  139. {$IFC UNDEFINED THINK_PASCAL}
  140.     uses
  141.         Types, Quickdraw, {, OSIntf, ToolIntf, PackIntf}
  142.         Menus, Windows, Memory, SegLoad, Scrap, ToolUtils, Fonts,{}
  143. {$IFC GENERATINGPOWERPC }
  144.         PPCTransSkelCallProcs, 
  145. {$ENDC}
  146.         Devices, TextEdit, Traps, Events, Dialogs, Resources, DiskInit,{}
  147.         AppleEvents, OSUtils, EPPC;
  148. {$ELSEC}
  149.     uses
  150.         InterfacesUI; {UPI interfaces in one file for Think Pascal}
  151. {$ENDC}
  152.  
  153.     procedure SkelInit (noMasters: integer; myGrowZone: ProcPtr);
  154.     procedure SkelMain;
  155.     procedure SkelWhoa;
  156.     procedure SkelClobber;
  157.     function SkelMenu (theMenu: MenuHandle; pSelect: ProcPtr; pClobber: ProcPtr; DrawBar: Boolean): Boolean;
  158.     function SkelHMenu (theMenu: MenuHandle; pSelect: ProcPtr; pClobber: ProcPtr): Boolean; {Added by Ingemar 22/8 -93}
  159.     procedure SkelRmveMenu (theMenu: MenuHandle);
  160.     procedure SkelApple (aboutTitle: Str255; aboutProc: ProcPtr);
  161.     function SkelWindow (theWind: WindowPtr; pMouse, pKey, pUpdate, pActivate, pClose, pClobber, pIdle: ProcPtr; frontOnly: Boolean): Boolean;
  162.     procedure SkelRmveWind (theWind: WindowPtr);
  163. {$IFC supportDialogs }
  164.     function SkelDialog (theDialog: DialogPtr; pEvent, pClose, pClobber, pFilter: ProcPtr): Boolean; {pFilter added by Ingemar 18/9-93}
  165.     procedure SkelRmveDlog (theDialog: DialogPtr);
  166. {$ENDC}
  167.     procedure SkelGrowBounds (theWind: WindowPtr; hLO, vLo, hHi, vHi: integer);
  168.     procedure SkelEventMask (mask: integer);
  169.     procedure SkelGetEventMask (var mask: integer);
  170.     procedure SkelBackground (p: ProcPtr);
  171.     procedure SkelGetBackground (var p: ProcPtr);
  172.     procedure SkelEventHook (p: ProcPtr);
  173.     procedure SkelGetEventHook (var p: ProcPtr);
  174. {$IFC supportDialogs }
  175.     procedure SkelDlogMask (mask: integer);
  176.     procedure SkelGetDlogMask (var mask: integer);
  177. {$ENDC}
  178. {Two new procedures for WNE-support, added by Ingemar 12/11-93}
  179.     procedure SkelSetSleep (newSleep: Longint);
  180.     procedure SkelSetMouseRgn (newMouseRgn: RgnHandle);
  181. {Utility function, added by Ingemar 13/7-94}
  182.     function FindWindowByRefcon (theRefCon: Longint): WindowPtr;
  183. {Suspend/resume, added by Ingemar 23/7 -94}
  184.     procedure SkelSetSuspendResume (p: ProcPtr);
  185.     function SkelGetSuspendResume: ProcPtr;
  186.  
  187. implementation
  188.  
  189. {Some stuff that are here to avoid including EPPC and AppleEvents, added by Ingemar 23/7 -94}
  190.     {const}
  191. {        kHighLevelEvent = 23;}
  192. {    function AEProcessAppleEvent (theEventRecord: EventRecord): OSErr;}
  193. {    inline}
  194. {        $303C, $021B, $A816;}
  195.  
  196.  
  197.  
  198.     const
  199.         mBarHeight = 20;    { menu bar height.  All window sizing}
  200.  
  201.         GrowZoneSize = 4000;        {  Size of memory to be freed when GrowZone Proc called }
  202.  
  203.         defaultSleep = 5;        { Added by Ingemar dec -93. SkelInit uses this value for sleep time.}
  204.                             { The programmer can change it with SkelSetSleep }
  205.  
  206. {    This window zooming stuff may need to be removed if you use the new Rom libraries        }
  207. {    if not, then you can add zooming without the overhead of the new Rom libs.  See TrackBox    }
  208. {     routine also.                                                                                            }
  209.  
  210. {    Window and Menu handler types, constants, variables.}
  211.  
  212. {    whList and mhList are the lists of window and menu handlers.}
  213. {    whClobOnRmve and mhClobOnRmve are true if the handler disposal proc}
  214. {    is to be called when a handler is removed.  They are temporarily set}
  215. {    false when handlers are installed for windows or menus that already}
  216. {    have handlers - the old handler is removed WITHOUT calling the}
  217. {    disposal proc.}
  218.  
  219. {    Default lower limits on window sizing of 80 pixels both directions is}
  220. {    sufficient to allow text windows room to draw a grow box and scroll}
  221. {    bars without having the thumb and arrows overlap.  These values may}
  222. {    be changed if such a constraint is undesirable with SkelGrowBounds.}
  223. {    Default upper limits are for the Macintosh, not the Lisa, but are set}
  224. {    per machine in SkelInit.}
  225.  
  226.     type
  227.         WHandlerPtr = ^WHandler;
  228.         WHandlerHnd = ^WHandlerPtr;
  229.         WHandler = record
  230.                 whWind: WindowPtr;    {window/dialog to be handled    }
  231.                 whClobber: ProcPtr;    { data structure disposal proc    }
  232.                 whMouse: ProcPtr;        { mouse-click handler proc        }
  233.                 whKey: ProcPtr;        { key-click handler proc            }
  234.                 whUpdate: ProcPtr;        { update handler proc                }
  235.                 whActivate: ProcPtr;    { activate event handler proc    }
  236.                 whClose: ProcPtr;        { close "event" handler proc        }
  237.                 whIdle: ProcPtr;            { main loop proc                    }
  238. {$IFC supportDialogs }
  239.                 whEvent: ProcPtr;        { dialog event proc                }
  240.                 whFilter: ProcPtr;        { dialog filter proc ADDED BY INGEMAR 18/9 -93}
  241. {$ENDC }
  242.                 whHasGrow: Boolean;    { can window grow?                }
  243.                 whGrow: Rect;            { limits on window sizing        }
  244.                 whSized: Boolean;        { true = window was resized    }
  245.                 whFrontOnly: Boolean;    { true = idle only when active    }
  246.                 whNext: WHandlerHnd;            { next window handler            }
  247.             end;
  248.  
  249.         MHandlerPtr = ^MHandler;
  250.         MHandlerHnd = ^MHandlerPtr;
  251.  
  252.         MHandler = record
  253.                 mhID: integer;                { menu id                                    }
  254.                 mhSelect: ProcPtr;            { item selection handler proc            }
  255.                 mhClobber: ProcPtr;        { menu disposal handler proc            }
  256.                 mhNext: MHandlerHnd;        { next menu handler                        }
  257.             end;
  258.  
  259.     var
  260.         whList: WHandlerHnd;                { list of menu handlers }
  261.         whClobOnRmve: Boolean;
  262.         growRect: Rect;
  263.         mhList: MHandlerHnd;
  264.         mhClobOnRmve: Boolean;
  265.  
  266. {    Variables for default Apple menu handler.  appleID is set to 1 if}
  267. {    SkelApple is called and is the id of the Apple menu, appleAboutProc}
  268. {    is the procedure to execute if there is an About... item and it's}
  269. {    chosen from the Apple menu.  If doAbout is true, then the menu}
  270. {    contains the About... item, otherwise it's just desk accessories.}
  271.  
  272.         appleMenu: MenuHandle;
  273.         appleID: integer;
  274.         appleAboutProc: ProcPtr;
  275.         doAbout: Boolean;
  276.  
  277. {    Miscellaneous}
  278.  
  279. {    screenPort points to the window manager port.}
  280.  
  281. {    doneFlag determines when SkelMain returns.  It is set by calling}
  282. {    SkelWhoa(), which the host does to request a halt.}
  283.  
  284. {    pBkgnd points to a background procedure, to be run during event}
  285. {    processing.  Set it with SkelBackground.  If nil, there's no}
  286. {    procedure.}
  287.  
  288. {    pEvent points to an event-inspecting hook, to be run whenever an}
  289. {    event occurs.  Set it with SkelEventHook.  If nil, there's no}
  290. {    procedure.}
  291.  
  292. {    eventMask controls the event types requested in the GetNextEvent}
  293. {    call in SkelMain.}
  294.  
  295. {    diskInitPt is the location at which the disk initialization dialog}
  296. {    appears, if an uninitialized disk is inserted.}
  297.  
  298.         screenPort: GrafPtr;
  299.         doneFlag: integer;
  300.         pBkgnd: ProcPtr;
  301.         pEvent: ProcPtr;
  302.         eventMask: integer;
  303.         diskInitPt: Point;
  304.  
  305. {Added by Ingemar 12/11-93, for WNE-support:}
  306.         WNEImplemented: Boolean;
  307.         sleepTicks: Longint;
  308.         mouseRgn: rgnHandle;
  309. {…and later, for suspend/resume support:}
  310.         gSuspendResumeHandler: ProcPtr;
  311.  
  312. {$IFC supportDialogs }
  313.  
  314. {    Events that are passed to dialogs.  Others are ignored.}
  315. {    Standard mask passes , mousedown, keydown, autokey, update,}
  316. {    activate and null events.  Null events are controlled by bit 0.}
  317.  
  318.         dlogEventMask: integer;
  319. {$ENDC}
  320.         pEventflag: Boolean;
  321.  
  322. {    "caching" global variables.  previous version would search down the window }
  323. {    list for every event it found.  Now, if the event happened to the same window }
  324. {    as last time, GetWDHandler will just do a simple compare }
  325. {    and return the last window handler.  This speeds up multiple window applications }
  326. {    immensely, at only a slight cost when you activate a new window (one }
  327. {    compare!)  If you don't like it, use the old version. }
  328.  
  329.         oldWindow: WindowPtr;
  330.         oldWDHandler: WHandlerHnd;
  331.  
  332. {    Global for built in "Grow Zone" function  }
  333.  
  334.         safetyHandle: Handle;
  335.  
  336.         myDitl: packed array[0..100] of byte;
  337.  
  338. { Rather than including the entire new ROM libraries, with all the other stuff you might not use    }
  339. { I've instead included just the Zoom box stuff here.  Depending on your status, you can either    }
  340. { leave things as they are, and only use zooming from the new Rom libs, or comment out the        }
  341. { calls, and include the new Rom libraries if you want to incorporate other new Rom calls        }
  342.  
  343.  
  344. { -------------------------------------------------------------------- }
  345. {                        Internal (private) Routines                                    }
  346. { -------------------------------------------------------------------- }
  347.  
  348.  
  349.  
  350. {    Get handler associated with user or dialog window.}
  351. {    Return nil if window doesn't belong to any known handler.}
  352. {    This routine is absolutely fundamental to TransSkel.}
  353.  
  354.     function GetWDHandler (theWind: WindowPtr): WHandlerHnd;
  355.  
  356.         var
  357.             h: WHandlerHnd;
  358.     begin
  359.         h := WhList;
  360.         GetWDHandler := nil;
  361.         if theWind = oldWindow then            {  caching code     }
  362.             GetWDHandler := oldWDHandler
  363.         else
  364.             while h <> nil do
  365.                 if h^^.whWind = theWind then
  366.                     begin
  367.                         oldWindow := theWind;            { Load in new values for new window }
  368.                         oldWDHandler := h;
  369.                         GetWDHandler := h;
  370.                         h := nil;
  371.                     end
  372.                 else
  373.                     h := WHandlerHnd(h^^.whNext);
  374.     end;
  375.  
  376. { Get Handler associated with user window.  Return nil if window doesn't}
  377. {  have a Handler. }
  378.  
  379.     function GetWHandler (theWind: WindowPtr): WHandlerHnd;
  380.  
  381.         var
  382.             h: WHandlerHnd;
  383.             myPeek: WindowPeek;
  384.  
  385.     begin
  386. {BUG FIXED by Ingemar 19/9-93. This function retured garbage when passed a dialog}
  387.         h := GetWDHandler(theWind);
  388.         myPeek := WindowPeek(theWind);
  389.         GetWHandler := nil; {default, moved up by Ingemar}
  390.         if h <> nil then
  391.             begin
  392.                 if mypeek^.windowKind <> dialogKind then
  393.                     GetWHandler := h;
  394.             end;
  395.     end;
  396.  
  397. {$IFC supportDialogs }
  398.  
  399. {    Get handler associated with dialog window.}
  400. {    Return nil if window doesn't belong to any known handler.}
  401.  
  402.     function GetDHandler (theDialog: WindowPtr): WHandlerHnd;
  403.  
  404.         var
  405.             h: WHandlerHnd;
  406.             myPeek: WindowPeek;
  407.  
  408.     begin
  409. {BUG FIXED by Ingemar 19/9-93. This function retured garbage when passed a non-dialog}
  410.         h := GetWDHandler(theDialog);
  411.         myPeek := WindowPeek(theDialog);
  412.         GetDHandler := nil; {default - moved up by Ingemar}
  413.         if h <> nil then
  414.             begin
  415.                 if mypeek^.windowKind = dialogKind then
  416.                     GetDHandler := h;
  417.             end;
  418.     end;
  419. {$ENDC}
  420.  
  421. {Two new routines added by Ingemar 12/11-93 for WNE support:}
  422.     procedure SkelSetSleep (newSleep: Longint);
  423.     begin
  424.         sleepTicks := newSleep;
  425.     end;
  426.     procedure SkelSetMouseRgn (newMouseRgn: RgnHandle);
  427.     begin
  428.         mouseRgn := newMouseRgn;
  429.     end;
  430.  
  431. {Utility routine added by Ingemar july 1994. Finds a window in the window list that has the same}
  432. {refCon as the value passed. This is useful for programs where several windows share window handlers.}
  433. {(To be precise, i needed it for a program where I have several connections to other Macs over Appletalk.)}
  434.     function FindWindowByRefcon (theRefCon: Longint): WindowPtr;
  435.         var
  436.             h: WHandlerHnd;
  437.     begin
  438.         FindWindowByRefcon := nil;
  439.         h := WhList;
  440.         while h <> nil do
  441.             if WindowPeek(h^^.whWind)^.refCon = theRefCon then
  442.                 begin
  443.                     FindWindowByRefcon := h^^.whWind;
  444.                     exit(FindWindowByRefcon);
  445.                 end
  446.             else
  447.                 h := WHandlerHnd(h^^.whNext);
  448.     end;
  449.  
  450.     procedure SkelSetSuspendResume (p: ProcPtr);
  451.     begin
  452.         gSuspendResumeHandler := p;
  453.     end;
  454.     function SkelGetSuspendResume: ProcPtr;
  455.     begin
  456.         SkelGetSuspendResume := gSuspendResumeHandler;
  457.     end;
  458.  
  459. {The following procedures are Pascal "glue" that allows Pascal to call a Procedure    }
  460. { from a ProcPtr.  It is similar to (*p) () construct used in the C dialect.  Different    }
  461. { procedures are necessary for the reason of Pascal's strongly typed parameter        }
  462. { list.  Fortunately, there are not too many calls which use different param lists        }
  463.  
  464. {The following procedures are Pascal "glue" that allows Pascal to call a Procedure    }
  465. { from a ProcPtr.  It is similar to (*p) () construct used in the C dialect.  Different    }
  466. { procedures are necessary for the reason of Pascal's strongly typed parameter        }
  467. { list.  Fortunately, there are not too many calls which use different param lists        }
  468.  
  469. {NOTE: Metrowerks Pascal supports function types, which makes it possible to pass}
  470. {ProcPtrs with type checking. That is MUCH better, so this is an interim solution.}
  471.  
  472. {$IFC GENERATINGPOWERPC }
  473. {$ELSEC}
  474.  
  475.     procedure CallpMouse (thePoint: Point; theTime: longint; theMods: integer; myProc: ProcPtr);
  476.  
  477.     inline
  478.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  479.         $4e90;
  480.  
  481.     procedure CallpKey (theChar: char; theMods: integer; myProc: ProcPtr);
  482.     inline
  483.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  484.         $4e90;
  485.  
  486.     procedure CallpEvent (theitem: integer; var theEvent: EventRecord; myProc: ProcPtr);
  487.     inline
  488.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  489.         $4e90;
  490.  
  491.     function CallotherEvent (var theEvent: EventRecord; myProc: ProcPtr): Boolean;
  492.     inline
  493.         $205f, $4e90;
  494.  
  495.     procedure CallpBoolean (myBool: Boolean; myProc: ProcPtr);
  496. { Two calls use Booleans as one parameter arguments.  This procedure handles    }
  497. { both of them.                                                                            }
  498.     inline
  499.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  500.         $4e90;
  501.  
  502.     procedure CallpInt (myInt: integer; myProc: ProcPtr);
  503. { Two calls use Booleans as one parameter arguments.  This procedure handles    }
  504. { both of them.                                                                            }
  505.     inline
  506.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  507.         $4e90;
  508.  
  509.     procedure CallpMenu (myMenu: MenuHandle; myProc: ProcPtr);
  510. { Handle removeal of menus.     }
  511.     inline
  512.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  513.         $4e90;
  514.  
  515.     procedure Callpnoarg (myProc: ProcPtr);
  516. { For all the Procedures that are called with no arguments                            }
  517.     inline
  518.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  519.         $4e90;
  520.  
  521. {ADDED BY INGEMAR 18/9-93 to support new dialog filters}
  522. {IS IT POSSIBLE to use this for functions?}
  523.     procedure Callpfilter (theDialog: DialogPtr; var theEvent: EventRecord; var result: Boolean; myProc: ProcPtr);
  524.     inline
  525.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  526.         $4e90;
  527.  
  528. {Some stuff that are here to avoid including EPPC and AppleEvents, added by Ingemar 23/7 -94}
  529.     const
  530.         kHighLevelEvent = 23;
  531.     function AEProcessAppleEvent (theEventRecord: EventRecord): OSErr;
  532.     inline
  533.         $303C, $021B, $A816;
  534.  
  535. {$ENDC}
  536.  
  537. {    General menu-handler.  Just passes selection to the handler's}
  538. {    select routine.  If the select routine is nil, selecting items from}
  539. {    the menu is a nop.}
  540.  
  541.     procedure DoMenuCommand (command: longint);
  542.  
  543.         var
  544.             menu: integer;
  545.             item: integer;
  546.             mh: MHandlerHnd;
  547.             p: ProcPtr;
  548.  
  549.     begin
  550.         menu := HiWrd(command);
  551.         item := LoWrd(command);
  552.         mh := mhList;
  553.         while (mh <> nil) do
  554.             begin
  555.                 p := mh^^.mhSelect;
  556.                 if ((menu = mh^^.mhID) and (p <> nil)) then
  557.                     begin
  558.                         callpInt(item, p);
  559.                         mh := nil;
  560.                     end
  561.                 else
  562.                     mh := mh^^.mhNext;
  563.             end;
  564.         HiliteMenu(0);
  565.     end;
  566.  
  567. {    Apple menu handler}
  568.  
  569. {    DoAppleItem:  If the first item was chosen, and there's an "About..."}
  570. {    item, call the procedure associated with it (if not nil).  If there}
  571. {    is no "About..." item or the item was not the first one, then open}
  572. {    the associated desk accessory.  The port is saved and restored}
  573. {    because OpenDeskAcc does not always preserve it correctly.}
  574.  
  575. {    DoAppleClobber disposes of the Apple menu.}
  576.  
  577.     procedure DoAppleItem (item: integer);
  578.  
  579.         var
  580.             curPort: GrafPtr;
  581.             str: Str255;
  582.             ignore: integer;
  583.             h: Handle;
  584.  
  585.     begin
  586.         if doAbout and (item = 1) then
  587.             begin
  588.                 if appleAboutProc <> nil then
  589.                     callpnoarg(appleAboutProc);
  590.             end
  591.         else
  592.             begin
  593.                 GetPort(curPort);
  594.                 GetMenuItemText(appleMenu, item, str);
  595.                 SetResLoad(false);
  596.                 h := GetNamedResource('DRVR', str);
  597.                 SetResLoad(true);
  598.                 if h <> nil then
  599.                     begin
  600.                         ReserveMem(GetResourceSizeOnDisk(h) + $1000);
  601.                         ignore := OpenDeskAcc(str);
  602.                     end;
  603.                 SetPort(curPort);
  604.             end;
  605.     end;
  606.  
  607.     procedure DoAppleClobber;
  608.     begin
  609.         DisposeMenu(appleMenu);
  610.     end;
  611.  
  612. { --------------------------------------------------------------------     }
  613. {                        Window-handler routing routines                            }
  614. {                                                                                        }
  615. {    Each routine sets the port to the handler's window before executing        }
  616. {    the handler procedure.                                                            }
  617. { --------------------------------------------------------------------     }
  618.  
  619.  
  620. {    Pass local mouse coordinates, click time, and the modifiers flag}
  621. {    word to the handler.  Should not be necessary to set the port, as}
  622. {       the click is passed to the active window's hander. }
  623.  
  624.     procedure DoMouse (h: WHandlerHnd; theEvent: EventRecord);
  625.  
  626.         var
  627.             p: ProcPtr;
  628.             thePt: Point;
  629.  
  630.     begin
  631.         if (h <> nil) then
  632.             begin
  633.                 p := h^^.whMouse;
  634.                 if p <> nil then
  635.                     begin
  636.                         thePt := theEvent.where;
  637.                         GlobalToLocal(thePt);
  638.                         callpMouse(thePt, theEvent.when, theEvent.modifiers, p);
  639.                     end;
  640.             end;
  641.     end;
  642.  
  643. {    Pass the character and the modifiers flag word to the handler.}
  644. {    Should not be necessary to set the port, as the  click is passed to the}
  645. {    active window's handler. }
  646.  
  647.     procedure DoKey (h: WHandlerHnd; ch: char; mods: integer);
  648.         var
  649.             p: ProcPtr;
  650.  
  651.     begin
  652.         if h <> nil then
  653.             begin
  654.                 p := h^^.whKey;
  655.                 if p <> nil then
  656.                     callpKey(ch, mods, p);
  657.             end;
  658.     end;
  659.  
  660. {    Call the window updating procedure, passing to it an indicator whether the}
  661. {    window has been resized or not.  Then clear the flag, assuming the update}
  662. {    proc took whatever action was necessary to respond to resizing.}
  663. {}
  664. {    If the handler doesn't have any update proc, the Begin/EndUpdate stuff}
  665. {    is still done, to clear the update region.  Otherwise the Window Manager }
  666. {    will keep generating update events for the window, stalling updates of}
  667. {    other windows.    }
  668.  
  669. {    Make sure to save and restore the port, as it's not always the active window}
  670. {    that's updated.    }
  671.  
  672.     procedure DoUpdate (h: WHandlerHnd);
  673.  
  674.         var
  675.             rh: WhandlerHnd;
  676.             p: ProcPtr;
  677.             updPort, tmpPort: GrafPtr;
  678.  
  679.     begin
  680.         rh := h;
  681.         if rh <> nil then
  682.             begin
  683.                 GetPort(tmpPort);
  684.                 updPort := rh^^.whWind;
  685.                 SetPort(updPort);
  686.                 BeginUpdate(updPort);
  687.                 p := rh^^.whUpdate;
  688.                 if p <> nil then
  689.                     begin
  690.                         callpBoolean(rh^^.whSized, p);
  691.                         rh^^.whSized := false;
  692.                     end;
  693.                 EndUpdate(updPort);
  694.                 SetPort(tmpPort);
  695.             end;
  696.     end;
  697.  
  698. {    Pass activate/deactivate notification to handler.  On activate, set the port to}
  699. {    the window coming active    }
  700.  
  701.     procedure DoActivate (h: WHandlerHnd; active: Boolean);
  702.  
  703.         var
  704.             p: ProcPtr;
  705.             savePort: GrafPtr;
  706.  
  707.     begin
  708.         if h <> nil then
  709.             begin
  710.                 GetPort(savePort);        {Fix by Ingemar, see below.}
  711. {                if active then}
  712.                 SetPort(h^^.whWind);
  713.                 p := h^^.whActivate;
  714.                 if p <> nil then
  715.                     callpBoolean(active, p);
  716.                 if not active then        {Fix by Ingemar 0996: TransEdit assumes that the current window is the current port even in deactivate!}
  717.                     SetPort(savePort);
  718.             end
  719.     end;
  720.  
  721. {    Execute a window handler's close proc.  The close box for handlers}
  722. {    for temp windows that want to remove themselves when the window}
  723. {    is closed can call SkelRmveWind to dispose of the window}
  724. {    and remove the handler from the window handler list.  Thus, windows}
  725. {    may be dynamically created and destroyed without filling up the}
  726. {    handler list with a bunch of invalid handlers.}
  727.  
  728. {    If the handler doesn't have a close proc, just hide the window.}
  729. {    The host should provide some way of reopening the window (perhaps}
  730. {    a menu selection).  Otherwise the window will be lost from user}
  731. {    control if it is hidden, since it won't receive user-initiated events.}
  732.  
  733. {    Since the close box of only the active window may be clicked, it}
  734. {    is not necessary to set the port . }
  735.  
  736. {    This is called both for regular and dialog windows.}
  737.  
  738.     procedure DoClose (h: WHandlerHnd);
  739.  
  740.         var
  741.             rh: WHandlerHnd;
  742.             p: ProcPtr;
  743.     begin
  744.         rh := h;
  745.         if rh <> nil then
  746.             begin
  747.                 p := rh^^.whClose;
  748.                 if (p <> nil) then
  749.                     callpnoarg(p)
  750.                 else
  751.                     HideWindow(rh^^.whWind);
  752.             end;
  753.     end;
  754.  
  755. {    Execute a window Handler's clobber proc.  This is called both for regular and dialog windows.}
  756. {    Must save, set and restore port, since any window (not just active one) may be clobbered    }
  757. {    at any time.}
  758. {}
  759. {    Don't need to check whether handler is nil, as in other handler procedures, since this is only}
  760. {    called by SkelRmveWind with a known valid handler.     }
  761.  
  762.     procedure DoClobber (h: WHandlerHnd);
  763.  
  764.         var
  765.             p: ProcPtr;
  766.             curPort: Grafptr;
  767.     begin
  768.         if (h <> nil) then
  769.             begin
  770.                 GetPort(curPort);
  771.                 SetPort(h^^.whWind);
  772.                 p := h^^.whClobber;
  773.                 if p <> nil then
  774.                     callpnoarg(p);
  775.                 SetPort(curPort);
  776.             end;
  777.     end;
  778.  
  779. {$IFC supportDialogs }
  780.  
  781. {    Handle event if it's for a dialog.  The event must be one of}
  782. {    those that is passed to dialogs according to dlogEventMask.}
  783. {    This mask can be set so that disk-inserts, for instance, don't}
  784. {    get eaten up.}
  785.  
  786.     function DoDialog (theEvent: EventRecord): Boolean;
  787.  
  788.         var
  789.             dh: WHandlerHnd;
  790.             theDialog: DialogPtr;
  791.             myDPeek: DialogPeek;
  792.             what: integer;
  793.             item: integer;
  794.             tmpPort: GrafPtr;
  795.             ignore: Boolean;
  796.             testme: longint;
  797.             hasfilter, filtered: Boolean;
  798.  
  799.     begin
  800.  
  801. {    handle command keys before they get to IsDialogEvent}
  802.  
  803.         what := theEvent.what;
  804.         testme := BSL(longint(1), what);
  805.         testme := BAnd(testme, longint(dlogEventMask));
  806.         if (((what = keydown) or (what = autokey)) and (BAnd(theEvent.modifiers, cmdkey) <> 0)) then
  807. { Bugfix by Ingemar above! }
  808.             begin
  809.                 DoMenuCommand(MenuKey(Char(BAnd(theEvent.message, charCodeMask))));
  810.                 DoDialog := true;
  811.             end
  812. {Filter procedure, Added by Ingemar 18/9 -93:}
  813.         else
  814.             begin
  815. {Check if theDialog has whFilter!}
  816. {Bugfix 941026: I used FrontWindow, which was really silly since that won't work}
  817. {for update events!}
  818.                 theDialog := DialogPtr(theEvent.message);
  819.                 dh := WHandlerHnd(GetDHandler(theDialog));
  820.                 filtered := false;
  821.                 hasfilter := dh <> nil;
  822.                 if hasfilter then
  823.                     hasfilter := dh^^.whFilter <> nil;
  824.                 if hasfilter then
  825.                     CallPFilter(theDialog, theEvent, filtered, dh^^.whFilter);
  826.                 DoDialog := filtered;
  827.                 if not filtered then
  828. {end of filter proc handling}
  829. {else}
  830.                     if testme > 0 then
  831.                         if IsDialogEvent(theEvent) then
  832.                             begin
  833.                                 if DialogSelect(theEvent, theDialog, item) then
  834.                                     begin
  835.                                         dh := WHandlerHnd(GetDHandler(theDialog));
  836.                                         if (dh <> nil) then
  837.                                             if (dh^^.whEvent <> nil) then
  838.                                                 begin
  839.                                                     GetPort(tmpPort);
  840.                                                     SetPort(theDialog);
  841.                                                     callpEvent(item, theEvent, dh^^.whEvent);
  842.                                                     SetPort(tmpPort);
  843.                                                 end;
  844.                                     end;
  845.                                 DoDialog := true;
  846.                             end
  847.                         else
  848.                             DoDialog := false;
  849.             end; {to match begin added with filter above}
  850.     end;
  851. {$ENDC}
  852.  
  853. { -------------------------------------------------------------------- }
  854. {                            Event-handling routines                        }
  855. { -------------------------------------------------------------------- }
  856.  
  857. {    Have either sized or zoomed the window.  Invalidate it to force}
  858. {    an update and set the 'resized' flag in the window handler true.}
  859. {    The port is assumed to be set to the port that changed size.        }
  860.  
  861.     procedure TriggerUpdate (h: WHandlerHnd; thePort: GrafPtr);
  862.  
  863.     begin
  864.         InvalRgn(thePort^.visRgn);
  865. {Changed by Ingemar 1/4-94. Was: InvalRect(thePort^.portRect);}
  866. {This is ok for rectangular windows, but all windows aren't rectangular!}
  867.         if (h <> nil) then
  868.             begin
  869.                 h^^.whSized := true;
  870.             end;
  871.     end;
  872.  
  873. {    Size a window.  If the window has a handler, use the grow limits}
  874. {    in the handler record, otherwise use the defaults.}
  875.  
  876. {    The portRect is invalidated to force an update event.    The handler's}
  877. {    update procedure should check the parameter passed to it to check}
  878. {    whether the window has changed size, if it needs to adjust itself to}
  879. {    the new size.  THIS IS A CONVENTION.  Update procs must notice grow}
  880. {    "events", there is no procedure specifically for such events.}
  881.  
  882. {    The clipping rectangle is not reset.  If the host application}
  883. {    keeps the clipping set equal to the portRect or something similar,}
  884. {    then it will have to arrange to treat window growing with more}
  885. {    care.}
  886. {}
  887. {    Since the grow region of only the active window may be clicked, it should}
  888. {    not be necessary to set the port.}
  889.  
  890.     procedure DoGrow (h: WHandlerHnd; thePort: GrafPtr; StartPt: Point);
  891.  
  892.         var
  893.             r: Rect;
  894.             growRes: longint;
  895.  
  896.     begin
  897.         if (h <> nil) then
  898.             begin
  899.                 r := h^^.whGrow;
  900.             end
  901.         else
  902.             r := growRect;
  903.         growRes := GrowWindow(thePort, startPt, r);
  904.         if growRes <> 0 then
  905.             begin
  906.                 SizeWindow(thePort, LoWrd(growRes), HiWrd(growRes), false);
  907.                 TriggerUpdate(h, thePort);
  908.             end;
  909.     end;
  910.  
  911.  
  912. {    Zoom the current window.  Very similar to DoGrow}
  913. {    Since the zoombox of only the active window may be clicked, it should not be necessary}
  914. {    to set the port.    }
  915.  
  916.     procedure DoZoom (h: WHandlerHnd; thePort: GrafPtr; partcode: integer);
  917.  
  918.     begin
  919.         ZoomWindow(thePort, partcode, false);
  920.         TriggerUpdate(h, thePort);
  921.     end;
  922.  
  923. {    General event handler}
  924.  
  925.     procedure DoEvent (theEvt: Eventrecord);
  926.  
  927.         var
  928.             theEvent: EventRecord;
  929.             evtPt: Point;
  930.             evtPort: GrafPtr;
  931.             evtPart: integer;
  932.             evtChar: char;
  933.             evtMods: integer;
  934.             h: WHandlerHnd;
  935.             r: Rect;
  936.             ignore: integer;
  937.  
  938.     begin
  939.         theEvent := theEvt;
  940. {$IFC supportDialogs }
  941.         if not (DoDialog(theEvent)) then
  942. {$ENDC}
  943.             begin
  944.                 evtPt := theEvent.where;
  945.                 evtMods := theEvent.modifiers; {Bug fixed by Ingemar 941027 - this statement was missing}
  946.                 case theEvent.what of
  947.                     nullEvent: 
  948.                         ;
  949.  
  950. {    Mouse click.  Get the window that the click occurred in, and the}
  951. {    part of the window.    Get WDHandler is called here, not GetWHandler, since}
  952. {    we need the handler for a window which might turn out to be a dialog window,}
  953. {    e.g., if the click is in a close box.}
  954.  
  955.                     mouseDown: 
  956.                         begin
  957.                             evtPart := FindWindow(evtPt, evtPort);
  958.                             h := GetWDHandler(evtPort);
  959.                             case evtPart of
  960.  
  961. {    Click in a desk accessory window.  Pass back to the system.}
  962.  
  963.                                 inSysWindow: 
  964.                                     SystemClick(theEvent, evtPort);
  965.  
  966. {    Click in menu bar.  Track the mouse and execute selected command,}
  967. {    if any.}
  968.  
  969.                                 inMenuBar: 
  970.                                     DoMenuCommand(MenuSelect(evtPt));
  971.  
  972. {    Click in grow box.  Resize window.}
  973.  
  974.                                 inGrow: 
  975.                                     DoGrow(h, evtPort, evtPt);
  976.  
  977. {    Click in title bar.  Drag the window around.  Leave at least}
  978. {    4 pixels visible in both directions.    Bug fix: The window, if not front, is}
  979. {    selected first to make sure it's at least activated (unless the command key is down - see Inside}
  980. {    Macintosh).  DragWindow seems to call StillDown first, so that clicks in drag regions while}
  981. {    machine is busy don't otherwise bring window to front if the mouse is already up by the time}
  982. {    DragWindow is called.}
  983.  
  984. {BUG??? Where is evtmods assigned???}
  985.  
  986.                                 inDrag: 
  987.                                     begin
  988.                                         if (evtPort <> FrontWindow) and (BAnd(evtmods, cmdKey) = 0) then
  989.                                             SelectWindow(evtPort);
  990.                                         r := screenPort^.portRect;
  991.                                         r.top := r.top + mBarHeight;        { Skip down past menu bar    }
  992.                                         InsetRect(r, 4, 4);
  993.                                         DragWindow(evtPort, evtPt, r);
  994.                                     end;
  995.  
  996. {    Click in close box.  Call the close proc if the window has one.}
  997.  
  998.                                 inGoAway: 
  999.                                     if (TrackGoAway(evtPort, evtPt)) then
  1000.                                         DoClose(GetWDHandler(evtPort));
  1001.  
  1002. {    Click in content region.  If the window wasn't frontmost (active),}
  1003. {    just select it, otherwise pass the click to the window's mouse}
  1004. {    click handler.}
  1005.  
  1006.                                 inContent: 
  1007.                                     if (evtPort <> FrontWindow) then
  1008.                                         SelectWindow(evtPort)
  1009.                                     else
  1010.                                         DoMouse(h, theEvent);
  1011.  
  1012. {    Click in zoom box.  Track the click and then zoom the window if}
  1013. {    necessary}
  1014.  
  1015.                                 inZoomin, inZoomOut: 
  1016.                                     if (TrackBox(evtPort, evtPt, evtPart)) then
  1017.                                         DoZoom(h, evtport, evtPart);
  1018.                                 otherwise
  1019.                                     ;
  1020.                             end;{mousedown}
  1021.                         end;
  1022.  
  1023. {    Key event.  If the command key was down, process as menu item}
  1024. {    selection, otherwise pass the character and the modifiers flags}
  1025. {    to the active window's key handler.}
  1026.  
  1027. {    If dialogs are supported, there's no check for command-key}
  1028. {    equivalents, since that would have been checked in DoDialog.}
  1029.  
  1030.                     keydown, autokey: 
  1031.                         begin
  1032.                             evtChar := char(BAnd(theEvent.message, charCodeMask));
  1033.                             evtMods := theEvent.modifiers;
  1034.                             if BAnd(evtMods, cmdKey) > 0 then
  1035.                                 DoMenuCommand(menuKey(evtChar))
  1036.                             else
  1037.                                 DoKey(GetWHandler(FrontWindow), evtChar, evtMods);
  1038.                         end;
  1039.  
  1040. {    Update a window.}
  1041.  
  1042.                     updateEvt: 
  1043.                         DoUpdate(GetWHandler(WindowPtr(theEvent.message)));
  1044.  
  1045. {    Activate or deactivate a window.}
  1046.  
  1047.                     activateEvt: 
  1048.                         DoActivate(GetWHandler(WindowPtr(theEvent.message)), (BAnd(theEvent.modifiers, activeFlag) <> 0));
  1049.  
  1050. {    handle inserts of uninitialized disks}
  1051.  
  1052.                     diskEvt: 
  1053.                         if (HiWrd(theEvent.message) <> noErr) then
  1054.                             begin
  1055.                                 DILoad;
  1056.                                 ignore := DIBadMount(diskInitPt, theEvent.message);
  1057.                                 DIUnload;
  1058.                             end;
  1059.  
  1060. { Handle suspend/resume and Apple Events, added by Ingemar 23/7 -94}
  1061.  
  1062.                     OSevt: 
  1063.                         if gSuspendResumeHandler <> nil then
  1064.                             if BAND(BROTL(theEvent.message, 8), $FF) = SuspendResumeMessage then
  1065.                                 CallPBoolean(BAnd(theEvent.message, 1) <> 0, gSuspendResumeHandler);
  1066.  
  1067. { Handle Apple Events, added by Ingemar 23/7 -94}
  1068.  
  1069.                     kHighLevelEvent: 
  1070.                         if AEProcessAppleEvent(theEvent) <> noErr then
  1071.                             ;
  1072.  
  1073.                     otherwise
  1074.                 end;
  1075.             end;
  1076.     end;
  1077.  
  1078. { -------------------------------------------------------------------- }
  1079. {                        Interface (public) Routines                        }
  1080. { -------------------------------------------------------------------- }
  1081.  
  1082.  
  1083. {    Initialize the various Macintosh Managers.}
  1084. {    Set default upper limits on window sizing.}
  1085. {    FlushEvents does NOT toss disk insert events, so that disks}
  1086. {    inserted while the application is starting up don't result}
  1087. {    in dead drives.}
  1088. {    NoMasters is the number of times to call MoreMasters.  gzProc is the address of a user - provided}
  1089. {    grow zone function procedure to call if memory gets tight.  Pass nil if none to be used.    }
  1090.  
  1091.     procedure SkelInit;
  1092.         var
  1093.             i: integer;
  1094.  
  1095.     begin
  1096.  
  1097. { For non-Lightspeed Pascal users, the following inits are included as a compile time option, }
  1098. {  See the $SETC definition at the beginning of the unit.  }
  1099.  
  1100. {$IFC UNDEFINED THINK_PASCAL }
  1101.         InitGraf(@qd.thePort);
  1102.         InitFonts;
  1103.         InitWindows;
  1104.         InitMenus;
  1105.         TEInit;
  1106.         InitDialogs(nil);
  1107.         MaxApplZone;
  1108. {$ENDC}
  1109.  
  1110.         FlushEvents(everyEvent - diskMask, 0);
  1111.         for i := 1 to noMasters do
  1112.             MoreMasters;
  1113.         if myGrowZone <> nil then
  1114.             SetGrowZone(myGrowZone);
  1115.  
  1116.         InitCursor;
  1117.  
  1118. {Added by Ingemar 12/11-93, for WNE-support:}
  1119.         WNEImplemented := NGetTrapAddress($60, ToolTrap) <> NgetTrapAddress($9F, ToolTrap);
  1120.         SkelSetSleep(defaultSleep);
  1121.         SkelSetMouseRgn(nil);
  1122. {End WNE-support}
  1123.  
  1124.         whList := nil;
  1125.         whClobOnRmve := true;
  1126.         SetRect(growRect, 80, 80, 512, 342 - mBarHeight);
  1127.         mhList := nil;
  1128.         mhClobOnRmve := true;
  1129.         appleID := 0;
  1130.         appleAboutProc := nil;
  1131.         doAbout := false;
  1132.         doneflag := 0;
  1133.         pBkgnd := nil;
  1134.         pEvent := nil;
  1135.         pEventflag := false;
  1136.         eventmask := everyEvent;
  1137.         diskInitPt.v := 120;
  1138.         diskInitPt.h := 100;
  1139. {$IFC supportDialogs }
  1140.         dlogEventMask := $16f;
  1141. {$ENDC}
  1142.  
  1143. {    Set upper limits of window sizing to machine screen size.  Allow}
  1144. {    for the menu bar.}
  1145.  
  1146.         GetWMgrPort(screenport);
  1147.         growRect.right := screenPort^.portRect.right;
  1148.         growRect.bottom := screenPort^.portRect.bottom - mBarHeight;
  1149.  
  1150. {    Set caching global variables to nil }
  1151.  
  1152.         oldWindow := nil;
  1153.         oldWDHandler := nil;
  1154.     end;
  1155.  
  1156. {    Main loop.}
  1157.  
  1158. {    Task care of DA's with SystemTask.}
  1159. {    Run background task if there is one.}
  1160. {    If there is an event, check for an event hook.  If there isn't}
  1161. {    one defined, or if there is but it returns false, call the}
  1162. {    general event handler.  (Hook returns true if TransSkel should}
  1163. {    ignore the event.)}
  1164. {    If no event, call the "no-event" handler for the front window and for}
  1165. {    any other windows with idle procedures that are always supposed}
  1166. {    to run.  This is done in such a way that it is safe for idle procs}
  1167. {    to remove the handler for their own window if they want (unlikely,}
  1168. {    but...)  This loop doesn't check whether the window is really}
  1169. {    a dialog window or not, but it doesn't have to, because such}
  1170. {    things always have a nil idle proc.}
  1171. {    }
  1172. {    doneFlag is reset upon exit.  This allows it to be called}
  1173. {    repeatedly, or recursively.}
  1174.  
  1175. {    Null events are looked at (in SkelMain)}
  1176. {    and passed to the event handler.  This is necessary to make sure}
  1177. {    DialogSelect gets called repeatedly, or the caret won't blink if}
  1178. {    a dialog has any editText items.  Null events are not passed to any event-inspecting hook that may}
  1179. {    be installed.}
  1180.  
  1181.     procedure SkelMain;
  1182.  
  1183.         var
  1184.             theEvent: EventRecord;
  1185.             wh, wh2: WHandlerHnd;
  1186.             w: WindowPtr;
  1187.             haveEvent, testpevent, testbool: Boolean;
  1188.             tmpPort: GrafPtr;
  1189.             p: ProcPtr;
  1190.  
  1191.     begin
  1192.         while (doneFlag = 0) do
  1193.             begin
  1194.                 if WNEImplemented then {Added by Ingemar 12/11-93, for WNE-support:}
  1195.                     begin
  1196.                         if (pBkgnd <> nil) then
  1197.                             callpnoarg(pBkgnd);
  1198.                         haveEvent := WaitNextEvent(eventMask, theEvent, sleepTicks, mouseRgn);
  1199.                     end
  1200.                 else
  1201.                     begin {Old way:}
  1202.                         SystemTask;
  1203.                         if (pBkgnd <> nil) then
  1204.                             callpnoarg(pBkgnd);
  1205.                         haveEvent := GetNextEvent(eventMask, theEvent);
  1206.                     end;
  1207.  
  1208.                 if (pEvent <> nil) then
  1209.                     testpevent := CallotherEvent(theEvent, pEvent)
  1210.                 else
  1211.                     testpevent := false;
  1212. { following line fixed from version 1.02 & 1.03     [but was still buggy! /Ingemar]}
  1213. {$IFC supportDialogs }
  1214.                 if (pEvent = nil) or (testpevent = false) then {haveEvent and <- This is wrong for modeless dialogs according to page 416? Changed by Ingemar 6/8 -93}
  1215. {$ELSEC}
  1216.                     if haveEvent and ((pEvent = nil) or (testpevent = false)) then { Old line. IFC'ad by Ingemar 6/8 -93}
  1217. {$ENDC}
  1218.                         DoEvent(theEvent);
  1219.                 if not haveEvent then
  1220.                     begin
  1221.                         wh := whList;
  1222.                         GetPort(tmpPort);
  1223.                         while (wh <> nil) do
  1224.                             begin
  1225.                                 wh2 := wh^^.whNext;
  1226.                                 w := wh^^.whWind;
  1227.                                 if ((w = FrontWindow) or not wh^^.whFrontOnly) then
  1228.                                     begin
  1229.                                         SystemTask;
  1230.                                         if (wh^^.whIdle <> nil) then
  1231.                                             begin
  1232.                                                 SetPort(wh^^.whWind);
  1233.                                                 p := wh^^.whIdle;
  1234.                                                 if (p <> nil) then
  1235.                                                     callpnoarg(p);
  1236.                                             end;
  1237.                                     end;
  1238.                                 wh := wh2;
  1239.                             end;
  1240.                         SetPort(tmpPort);
  1241.                     end;
  1242.             end;
  1243.         doneFlag := 0;
  1244.     end;
  1245.  
  1246. {    Tell SkelMain to stop}
  1247.  
  1248.     procedure SkelWhoa;
  1249.     begin
  1250.         doneFlag := 1;
  1251.     end;
  1252.  
  1253. {    Clobber all the menu, window and dialog handlers}
  1254.  
  1255.     procedure SkelClobber;
  1256.  
  1257.  
  1258.     begin
  1259.         oldWDHandler := nil;
  1260.         oldWindow := nil;
  1261.         while (whList <> nil) do
  1262.             begin
  1263.                 SkelRmveWind(whList^^.whWind);
  1264.             end;
  1265.         while (mhList <> nil) do
  1266.             begin
  1267.                 SkelRmveMenu(GetMenuHandle(mhList^^.mhID));
  1268.             end;
  1269.     end;
  1270.  
  1271. { -------------------------------------------------------------------- }
  1272. {                        Menu-handler interface routines                            }
  1273. { -------------------------------------------------------------------- }
  1274.  
  1275.  
  1276.  
  1277.  
  1278. {    Install handler for a menu.  Remove any previous handler for it.}
  1279. {    Pass the following parameters:}
  1280.  
  1281. {    theMenu    Handle to the menu to be handled.  Must be created by host.}
  1282. {    pSelect    Proc that handles selection of items from menu.  If this is}
  1283. {            nil, the menu is installed, but nothing happens when items}
  1284. {            are selected from it.}
  1285. {    pClobber Proc for disposal of handler's data structures.  Usually}
  1286. {            nil for menus that remain in menu bar until program}
  1287. {            termination.}
  1288.  
  1289. {    The menu is installed and drawn in the menu bar.}
  1290.  
  1291. {     Return false if no handler could be allocated, true if successful. }
  1292.  
  1293.     function CommonSkelMenu (theMenu: MenuHandle; pSelect: ProcPtr; pClobber: ProcPtr): Boolean;
  1294.         var
  1295.             mh: MHandlerHnd;
  1296.             myHand: Handle;
  1297.     begin
  1298.         mhClobOnRmve := false;
  1299.         SkelRmveMenu(theMenu);
  1300.         mhClobOnRmve := true;
  1301.         myHand := NewHandle(Sizeof(MHandler));
  1302.         CommonSkelMenu := false;
  1303.         if myHand <> nil then
  1304.             begin
  1305.                 CommonSkelMenu := true;                    { show we really got the memory }
  1306.                 mh := MHandlerHnd(myHand);
  1307.                 mh^^.mhNext := mhList;
  1308.                 mhList := MHandlerHnd(myHand);
  1309.                 mh^^.mhID := theMenu^^.menuID;    { get menu id number }
  1310.                 mh^^.mhSelect := pSelect;            { install selection handler }
  1311.                 mh^^.mhClobber := pClobber;        { install disposal handler }
  1312.             end;
  1313.     end;
  1314.  
  1315. {  Install handler for a normal menu }
  1316.  
  1317.     function SkelMenu;
  1318.         var
  1319.             success: Boolean;
  1320.     begin
  1321.         success := CommonSkelMenu(theMenu, pSelect, pClobber);
  1322.         SkelMenu := success;
  1323.         if success then
  1324.             begin
  1325.                 InsertMenu(theMenu, 0);            { put menu at end of menu bar }
  1326.                 if DrawBar then
  1327.                     DrawMenuBar;
  1328.             end;
  1329.     end;
  1330.  
  1331. {    Install handler for a hiearcical menu.  Almost same as above.}
  1332. {    Added by Ingemar 22/8 -93}
  1333.  
  1334.     function SkelHMenu;
  1335.         var
  1336.             success: Boolean;
  1337.     begin
  1338.         success := CommonSkelMenu(theMenu, pSelect, pClobber);
  1339.         SkelHMenu := success;
  1340.         if success then
  1341.             begin
  1342.                 InsertMenu(theMenu, -1);            { put menu at end of menu bar }
  1343.             end;
  1344.     end;
  1345.  
  1346.  
  1347. {    Remove a menu handler.  This calls the handler's disposal routine}
  1348. {    and then takes the handler out of the handler list and disposes}
  1349. {    of it.}
  1350.  
  1351. {    Note that the menu MUST be deleted from the menu bar before calling}
  1352. {    the clobber proc, because the menu bar will end up filled with}
  1353. {    garbage if the menu was allocated with NewMenu (see discussion of}
  1354. {    DisposeMenu in Menu Manager section of Inside Macintosh).}
  1355.  
  1356.     procedure SkelRmveMenu;
  1357.  
  1358.         var
  1359.             mID: integer;
  1360.             h, h2: MHandlerHnd;
  1361.             p: ProcPtr;
  1362.             returnflag: Boolean;
  1363.  
  1364.     begin
  1365.         mID := theMenu^^.menuID;
  1366.         returnflag := false;
  1367.         if mhlist <> nil then
  1368.             begin
  1369.                 if mhList^^.mhID = mID then
  1370.                     begin
  1371.                         h2 := mhlist;
  1372.                         mhList := h2^^.mhNext;
  1373.                     end
  1374.                 else
  1375.                     begin
  1376.                         h := mhList;
  1377.                         while (h <> nil) and not returnflag do
  1378.                             begin
  1379.                                 h2 := h^^.mhNext;
  1380.                                 if (h2 = nil) then
  1381.                                     begin
  1382.                                         h := nil;
  1383.                                         returnflag := true;
  1384.                                     end
  1385.                                 else if h2^^.mhID = mID then
  1386.                                     begin
  1387.                                         h^^.mhNext := h2^^.mhNext;
  1388.                                         h := nil;
  1389.                                     end;
  1390.                                 if h <> nil then
  1391.                                     h := h2;
  1392.                             end;
  1393.                     end;
  1394.                 if not returnflag then
  1395.                     begin
  1396.                         DeleteMenu(mID);
  1397.                         DrawMenuBar;
  1398.                         p := h2^^.mhClobber;
  1399.                         if mhClobOnRmve and (p <> nil) then
  1400.                             callpMenu(theMenu, p);
  1401.                         DisposeHandle(Handle(h2));
  1402.                     end;
  1403.             end;
  1404.     end;
  1405.  
  1406. {    Install a handler for the Apple menu.}
  1407.  
  1408. {    SkelApple is called if TransSkel is supposed to handle the apple}
  1409. {    menu itself.  The title is the title of the first item.  If nil,}
  1410. {    then only desk accessories are put into the menu.  If not nil, then}
  1411. {    the title is entered as the first item, followed by a gray line,}
  1412. {    then the desk accessories.}
  1413.  
  1414. {     SkelApple does not cause the menubar to be drawn, so if the Apple menu is the only menu, }
  1415. {    DrawMenuBar must be called afterward.}
  1416.  
  1417. {    No value is returned, unlike SkelMenu.  It is assumed that SkelApple will be called so early in the}
  1418. {    application that the call to SkelMenu is virtually certain to succeed.  }
  1419.  
  1420.     procedure SkelApple;
  1421.  
  1422.         var
  1423.             appleTitle: Str255;
  1424.             dummy: boolean;
  1425.     begin
  1426.         appleTitle := ' ';
  1427.         appleTitle[1] := char($14);
  1428.         appleID := 1;
  1429.         AppleMenu := NewMenu(appleID, appleTitle);
  1430.         if aboutTitle <> '' then
  1431.             begin
  1432.                 doAbout := true;
  1433.                 AppendMenu(appleMenu, aboutTitle);
  1434.                 AppendMenu(appleMenu, '(-');
  1435.                 AppleAboutProc := aboutProc;
  1436.             end;
  1437.         AppendResMenu(appleMenu, 'DRVR');
  1438.         dummy := SkelMenu(appleMenu, @DoAppleItem, @DoAppleClobber, false);
  1439.     end;
  1440.  
  1441. { -------------------------------------------------------------------- }
  1442. {                    Window-handler interface routines                    }
  1443. { -------------------------------------------------------------------- }
  1444.  
  1445.  
  1446.  
  1447. {    Install handler for a window.  Remove any previous handler for it.}
  1448. {    Pass the following parameters:}
  1449.  
  1450. {    theWind    Pointer to the window to be handled.  Must be created by host.}
  1451. {    pMouse    Proc to handle mouse clicks in window.  The proc will be}
  1452. {            passed the point (in local coordinates), the time of the}
  1453. {            click, and the modifier flags word.}
  1454. {    pKey    Proc to handle key clicks in window.  The proc will be passed}
  1455. {            the character and the modifier flags word.}
  1456. {    pUpdate    Proc for updating window.  TransSkel brackets calls to update}
  1457. {            procs with calls to BeginUpdate and EndUpdate, so the visRgn}
  1458. {            is set up correctly.  A flag is passed indicating whether the}
  1459. {            window was resized or not.  BY CONVENTION, the entire portRect}
  1460. {            is invalidated when the window is resized.  That way, the}
  1461. {            handler's update proc can redraw the entire content region}
  1462. {            without interference from BeginUpdate/EndUpdate.  The flag}
  1463. {            is set to false after the update proc is called; the}
  1464. {            assumption is made that it will notice the resizing and}
  1465. {            respond appropriately.}
  1466. {    pActivate Proc to execute when window is activated or deactivated.}
  1467. {            A boolean is passed to it which is true if the window is}
  1468. {            coming active, false if it's going inactive.}
  1469. {    pClose    Proc to execute when mouse clicked in close box.  Useful}
  1470. {            mainly to temp window handlers that want to know when to}
  1471. {            self-destruct (with SkelRmveWind).}
  1472. {    pClobber Proc for disposal of handler's data structures}
  1473. {    pIdle    Proc to execute when no events are pending.}
  1474. {    frontOnly True if pIdle should execute on no events only when}
  1475. {            theWind is frontmost, false if executes all the time.  Note}
  1476. {            that if it always goes, everything else may be slowed down!}
  1477.  
  1478. {    If a particular procedure is not needed (e.g., key events are}
  1479. {    not processed by a handler), pass nil in place of the appropriate}
  1480. {    procedure address.}
  1481.  
  1482. {    Return false  if no handler could be allocated, true if successful.}
  1483.  
  1484.     function SkelWindow;
  1485.  
  1486.         var
  1487.             hHand: WhandlerHnd;
  1488.  
  1489.     begin
  1490.         whClobOnRmve := false;
  1491.         SkelRmveWind(theWind);
  1492.         whClobOnRmve := true;
  1493.  
  1494. {    Get new handler, attach to list of handlers.  It is attached to the beginning of the list, which is simpler;}
  1495. {    the order should be irrelevant to the hose, anyway. }
  1496.  
  1497.         hHand := WHandlerHnd(NewHandle(Sizeof(WHandler)));
  1498.         SkelWindow := false;
  1499.         if hHand <> nil then
  1500.             begin
  1501.                 hHand^^.whNext := whList;
  1502.                 whList := hHand;
  1503.                 with hHand^^ do
  1504.                     begin
  1505.                         SkelWindow := true;                    { Show that we got the memory }
  1506.                         whWind := theWind;
  1507.                         whMouse := pMouse;
  1508.                         whKey := pKey;
  1509.                         whUpdate := pUpdate;
  1510.                         whActivate := pActivate;
  1511.                         whClose := pClose;
  1512.                         whClobber := pClobber;
  1513.                         whIdle := pIdle;
  1514.                         whFrontOnly := frontOnly;
  1515.                         whSized := false;
  1516.                         whGrow := GrowRect;
  1517.                     end;
  1518.             end;
  1519.         SetPort(theWind); {Is this allowed for hidden windows? I thought so, but… /Ingemar, dec 93}
  1520.     end;
  1521.  
  1522. {    Remove a window handler.  This calls the handler's disposal routine}
  1523. {    and then takes the handler out of the handler list and disposes}
  1524. {    of it.}
  1525.  
  1526. {    SkelRmveWind is also called by SkelRmveDlog.}
  1527.  
  1528. {    Note that if the window cache variable is set to the window whose handler is being clobbered, the }
  1529. {    variable must be zeroed.    }
  1530.  
  1531.     procedure SkelRmveWind;
  1532.  
  1533.         var
  1534.             h, h2: WHandlerHnd;
  1535.             returnflag: Boolean;
  1536.  
  1537.     begin
  1538.         if theWind = oldWindow then
  1539.             begin
  1540.                 oldWindow := nil;
  1541. {•    oldWDHandler := nil;•}
  1542.             end;
  1543.  
  1544.         if (whList <> nil) then
  1545.             begin
  1546.                 returnflag := false;
  1547.                 if whList^^.whWind = theWind then
  1548.                     begin
  1549.                         h2 := whlist;
  1550.                         whList := whList^^.whNext;
  1551.                     end
  1552.                 else
  1553.                     begin
  1554.                         h := whList;
  1555.                         while (h <> nil) and not returnflag do
  1556.                             begin
  1557.                                 h2 := h^^.whNext;
  1558.                                 if (h2 = nil) then
  1559.                                     begin
  1560.                                         h := nil;
  1561.                                         returnflag := true;
  1562.                                     end
  1563.                                 else if h2^^.whWind = theWind then
  1564.                                     begin
  1565.                                         h^^.whNext := h2^^.whNext;
  1566.                                         h := nil;
  1567.                                     end;
  1568.                                 if h <> nil then
  1569.                                     h := h2;
  1570.                             end;
  1571.                     end;
  1572.                 if not returnflag then
  1573.                     begin
  1574.                         if (whClobOnRmve) then
  1575.                             DoClobber(h2);
  1576.                         DisposeHandle(Handle(h2));
  1577.                     end;
  1578.             end;
  1579.     end;
  1580.  
  1581. {$IFC supportDialogs }
  1582.  
  1583. { -------------------------------------------------------------------- }
  1584. {                    Dialog-handler interface routines                                }
  1585. { -------------------------------------------------------------------- }
  1586.  
  1587.  
  1588.  
  1589. {    Install a dialog handler.  Remove any previous handler for it.}
  1590. {    SkelDialog calls SkelWindow as a subsidiary to install a window}
  1591. {    handler, then sets the event procedure on return.}
  1592.  
  1593. {    Pass the following parameters:}
  1594.  
  1595. {    theDialog    Pointer to the dialog to be handled.  Must be created}
  1596. {            by host.}
  1597. {    pEvent    Event-handling proc for dialog events.}
  1598. {    pClose    Proc to execute when mouse clicked in close box.  Useful}
  1599. {            mainly to dialog handlers that want to know when to}
  1600. {            self-destruct (with SkelRmveDlog).}
  1601. {    pClobber Proc for disposal of handler's data structures}
  1602.  
  1603. {    If a particular procedure is not needed, pass nil in place of}
  1604. {    the appropriate procedure address.}
  1605.  
  1606. {    Return false if no handler could be allocated, true if successful.}
  1607.  
  1608.     function SkelDialog;
  1609.  
  1610.         var
  1611.             wh: WHandlerHnd;
  1612.             aBool: Boolean;
  1613.  
  1614.     begin
  1615.         aBool := SkelWindow(theDialog, nil, nil, nil, nil, pClose, pClobber, nil, false);
  1616.         if aBool <> false then
  1617.             begin
  1618.                 wh := GetWDHandler(theDialog);
  1619.                 wh^^.whEvent := pEvent;
  1620. {Added by Ingemar 18/9 -93:}
  1621.                 wh^^.whFilter := pFilter; {Install a filter function to be called *before* IsDialogEvent!}
  1622.             end;
  1623.         SkelDialog := aBool;
  1624.     end;
  1625.  
  1626. {    Remove a dialog and its handler}
  1627.  
  1628.     procedure SkelRmveDlog;
  1629.  
  1630.     begin
  1631.         SkelRmveWind(theDialog);
  1632.     end;
  1633. {$ENDC}
  1634. { -------------------------------------------------------------------- }
  1635. {                    Miscellaneous interface routines                    }
  1636. { -------------------------------------------------------------------- }
  1637.  
  1638.  
  1639. {    Override the default sizing limits for a window, or, if theWind}
  1640. {    is nil, reset the default limits used by SkelWindow.}
  1641.  
  1642.     procedure SkelGrowBounds;
  1643.  
  1644.         var
  1645.             h: WHandlerHnd;
  1646.             r: Rect;
  1647.  
  1648.     begin
  1649.         if theWind = nil then
  1650.             SetRect(growRect, hLo, vLo, hHi, vHi)
  1651.         else
  1652.             begin
  1653.                 h := GetWHandler(theWind);
  1654.                 if h <> nil then
  1655.                     begin
  1656.                         SetRect(r, hLo, vLo, hHi, vHi);
  1657.                         h^^.whGrow := r;
  1658.                     end;
  1659.             end;
  1660.     end;
  1661.  
  1662. {    Set the event mask.}
  1663.  
  1664.     procedure SkelEventMask;
  1665.  
  1666.     begin
  1667.         eventMask := mask;
  1668.     end;
  1669.  
  1670. {    Return the event mask.}
  1671.  
  1672.     procedure SkelGetEventMask;
  1673.  
  1674.     begin
  1675.         mask := eventMask;
  1676.     end;
  1677.  
  1678. {    Install a background task.  If p is nil, the current task is}
  1679. {    disabled.}
  1680.  
  1681.     procedure SkelBackground;
  1682.  
  1683.     begin
  1684.         pBkgnd := p;
  1685.     end;
  1686.  
  1687. {    Return the current background task.  Return nil if none.}
  1688.  
  1689.     procedure SkelGetBackground;
  1690.     begin
  1691.         p := pBkgnd;
  1692.     end;
  1693.  
  1694. {    Install an event-inspecting hook.  If p is nil, the hook is}
  1695. {    disabled.}
  1696.  
  1697.     procedure SkelEventHook;
  1698.  
  1699.     begin
  1700.         pEvent := p;
  1701.     end;
  1702.  
  1703.     procedure SkelGetEventHook;
  1704.  
  1705.     begin
  1706.         p := pEvent;
  1707.     end;
  1708. {$IFC supportDialogs }
  1709.  
  1710. {    Set the mask for event types that will be passed to dialogs.}
  1711. {    Bit 1 is always set, so that null events will be passed.}
  1712. {    If this is not done, the caret does not blink in editText items.}
  1713.  
  1714.     procedure SkelDlogMask;
  1715.  
  1716.     begin
  1717.         dlogEventMask := BitOr(mask, 1);
  1718.     end;
  1719.  
  1720. {    Return the current dialog event mask.}
  1721.  
  1722.     procedure SkelGetDlogMask;
  1723.  
  1724.     begin
  1725.         mask := dlogEventMask;
  1726.     end;
  1727. {$ENDC}
  1728. end.